﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interfleet.SE.UICompiler.Core
{
    /// <summary>
    /// This class has static members that represents the available 
    /// compile types. Each type corresponds to a specific compiler.
    /// </summary>
    public class CompileType : CompileTypeBase
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="supplier"><see cref="CompileSupplier"/> of this compile type</param>
        /// <param name="entityType"><see cref="CompileEntityType"/> of this compile type</param>
        /// <param name="requiresCompileOption">True if compileoption is required</param>
        public CompileType(CompileSupplier supplier, CompileEntityType entityType, bool requiresCompileOption)
            : base(supplier, entityType, requiresCompileOption)
        {

        }

        /// <summary>
        /// Returns a <see cref="CompileType"/> corresponding to the supplied <see cref="CompileSupplier"/>
        /// </summary>
        /// <param name="supplier">The requested <see cref="CompileSupplier"/></param>
        /// <returns>A <see cref="CompileType"/></returns>
        public static CompileType FromSupplier(CompileSupplier supplier)
        {
            switch (supplier)
            {
                case CompileSupplier.ClosureCompilerLocal:
                    return CompileType.ClosureCompilerLocal;
                case CompileSupplier.ClosureCompilerREST:
                    return CompileType.ClosureCompilerREST;
                case CompileSupplier.YUICompressorCss:
                    return CompileType.YUICompressorCss;
                default:
                    throw new InvalidOperationException("Non supported CompileSupplier: " + supplier.ToString());
            }
        }

        /// <summary>
        /// Parses a string which must correspond to a <see cref="CompileSupplier"/> value to
        /// and instance of <see cref="CompileType"/>
        /// </summary>
        /// <param name="supplierString">A <see cref="System.String"/> corresponding to a <see cref="CompileSupplier"/> value</param>
        /// <returns>The corresponding <see cref="CompileType"/></returns>
        public static CompileType FromString(string supplierString)
        {
            var supplierType = (CompileSupplier)Enum.Parse(typeof(CompileSupplier), supplierString);
            return FromSupplier(supplierType);
        }

        /// <summary>
        /// Compilation of JavaScript will be done via the locally installed Java
        /// application of Closure Compiler
        /// </summary>
        /// <remarks>
        /// This compile type requires Java 6 or higher to be installed.
        /// </remarks>
        public static CompileType ClosureCompilerLocal
        {
            get
            {
                return new CompileType(CompileSupplier.ClosureCompilerLocal, CompileEntityType.JavaScript, true);
            }
        }

        /// <summary>
        /// Compilation of JavaScript will be done via the REST API of Closure Compiler
        /// </summary>
        /// <remarks>
        /// This compile type requires access to the Internet.
        /// </remarks>
        public static CompileType ClosureCompilerREST
        {
            get
            {
                return new CompileType(CompileSupplier.ClosureCompilerREST, CompileEntityType.JavaScript, true);
            }
        }

        /// <summary>
        /// Compilation of css will be done via Yahoo UI Compressor
        /// </summary>
        public static CompileType YUICompressorCss
        {
            get
            {
                return new CompileType(CompileSupplier.YUICompressorCss, CompileEntityType.Css, false);
            }
        }

        /// <summary>
        /// Compilation of javascript will be done via Yahoo UI Compressor
        /// </summary>
        public static CompileType YUICompressorJavaScript
        {
            get
            {
                return new CompileType(CompileSupplier.YUICompressorJavaScript, CompileEntityType.JavaScript, false);
            }
        }
    }
}
